package wrappers

import (
	"context"
	//"fmt"
	"github.com/afex/hystrix-go/hystrix"
	"github.com/micro/go-micro/v2/client"
	//"github.com/micro/go-plugins/wrapper/breaker/hystrix"
	"gomicro/webGoMicro/webMessage"
)

type WebClientWrapper struct {
	client.Client
}

func (w *WebClientWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
	cmdName := req.Service()+"."+req.Endpoint()
	commandConfig :=hystrix.CommandConfig{
		Timeout: 1000,
		// RequestVolumeThreshold: 2,
		// ErrorPercentThreshold: 50,
		// SleepWindow: 5000,
	}
	hystrix.ConfigureCommand(cmdName, commandConfig)
	return hystrix.Do(cmdName, func() error {
		return w.Client.Call(ctx, req, rsp, opts...)
	},func(e error) error{
		switch res := rsp.(type) { 
		case *webMessage.State:
			res.Code = "400"
			res.Message = "服务降级"
		case *webMessage.StudentListResponse:
			var qsr = webMessage.QueStudentResponse{
				Id:"120",
				Name:"熔断",
				Birth:"0000-00-00",
				Sex:"Sorry",
			}
			res.StudentList = append(res.StudentList, &qsr)
		default:
			
		}

		return nil
	})
}

func NewClientWrapper() client.Wrapper {
	return func(c client.Client) client.Client {
		return &WebClientWrapper{c}
	}
}